package com.qcby.tomcat;

import com.qcby.conf.ServletConfigMapping;
import com.qcby.servlet.HttpServlet;
import com.qcby.util.ResponseUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/************************************
 * @PACKAGE : com.qcby.tomcat
 * @Author    :  叫我老伯
 * @Date    : 2022/7/1 8:43
 * @Desc    : 实现两个接口  解析数据
 ***********************************/
public class MyTomcat {
    Request request = new Request();

    //获取类对象生成servlet对象
    public void dispatch(Request request,Response response) throws  Exception {
        //通过hashMap当中得key值获取到value值
        Class<HttpServlet> servletClass = ServletConfigMapping.classMap.get(request.getUrl());
        if(servletClass !=null){
            //生成对象
            HttpServlet servlet = servletClass.newInstance(); //生成servlet对象
            servlet.service(request,response);
        }
    }

    //定义一个主类——启动tomcat得主方法
    public void startUp() throws Exception{
        //加载servlet信息
        ServletConfigMapping.initServlet(); //启动阶段

        //定义ServletSocket8080端口
        ServerSocket servletSocket = new ServerSocket(8080);
        //监听
        while(true){
            Socket socket = servletSocket.accept();

            new Thread(new Runnable() {

                @Override
                public void run() {
                    try{
                        handler(socket);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    //解析用户请求数据，解析输入流
    public void handler(Socket socket) throws Exception {
        InputStream inputStream = socket.getInputStream();
        getInput(inputStream);  //解析http请求
        //获取输出通道、获取输出流
        Response response = new Response(socket.getOutputStream());
        //判断有没有servlet对象
        if(request.getUrl().equals("")){
            //放回404
            response.GetWrite(ResponseUtil.getResponseHeader404());
        }else if(ServletConfigMapping.classMap.get(request.getUrl()) == null){ //通过key得到value（类对象）
            //访问的是静态资源
            response.GetWriteHtml(request.getUrl());
        }else {
            //访问的是动态资源
            dispatch(request,response);
        }
    }

    //GET / HTTP/1.1
    public void getInput(InputStream inputStream) throws IOException {
        int count = 0;
        while (count ==0){
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String content = new String(bytes);
//        System.out.println(content);
        if (content.equals("")){
            System.out.println("您有一个空请求------------");
        }else {
            String firstLine = content.split("\\n")[0];
            request.setMethod(firstLine.split("\\s")[0]);
            request.setUrl(firstLine.split("\\s")[1]);
        }
    }

    public static void main(String[] args) throws Exception {
        MyTomcat myTomcat = new MyTomcat();
        myTomcat.startUp();
    }

}
